local mbut = CreateFrame("button", nil, InboxFrame, "UIPanelButtonTemplate")
mbut:SetPoint("BOTTOM", 0, 90)
mbut:SetWidth(100)
mbut:SetHeight(22)
mbut:SetText("Open All")

local processing = false


local function OnEvent()
	if(not MailFrame:IsShown()) then return end

	local num = GetInboxNumItems()

	if(processing) then
		if(num==0) then
			MiniMapMailFrame:Hide()
			processing = false
			return
		end

		for i = num, 1, -1 do
			local _, _, _, _, money, COD, _, item = GetInboxHeaderInfo(i)
			if(item and COD<1) then
				TakeInboxItem(i)
				return
			end
			if(money>0) then
				TakeInboxMoney(i)
				return
			end
		end
	end
end

mbut:RegisterEvent("MAIL_INBOX_UPDATE")
mbut:SetScript("OnEvent", OnEvent)
mbut:SetScript("OnClick", function(self) if not processing then processing = true OnEvent() end end)
mbut:SetScript("OnHide", function(self) processing = false end)